<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head th:replace="dashboard/header :: header"></head>

<body class="gray-bg">
	
	<link rel="stylesheet" th:href="@{/asserts/js/plugins/ztree/css/metroStyle/metroStyle.css}" type="text/css">
	
	<div class="container-div">
		<div class="row">
			 <div class="card col-lg-3">
			 	  <!-- 
			 	  <div class="card-body">
                   		<ul id="treeDemo" class="ztree"></ul>	
                  </div>
			 	   -->
                  
                  <div class="box box-main  search-collapse">
						<div class="box-header">
							<div class="box-title">
								<i class="fa icon-grid"></i> 组织机构
							</div>
							<div class="box-tools pull-right">
								<button type="button" class="btn btn-box-tool" id="btnAdd" title="添加部门" onclick="openDept()"><i class="fa fa-plus"></i></button>
								<button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新部门"><i class="fa fa-refresh"></i></button>
							</div>
						</div>
						<div class="ui-layout-content">
							<ul id="treeDemo" class="ztree"></ul>	
						</div>
					</div>
					
             </div>
			 <div class="col-lg-9" style="float:left;">
				<div class="col-sm-12 search-collapse">
					<form id="operlog-form">
						<div class="select-list">
							<ul>
								<li>
									<label>所属应用： </label><input type="text" name="condition[applicationId]"/>
								</li>
								<li>
									<label>用户名： </label><input type="text" name="condition[name|like]"/>
								</li>
								<li>
									<label>登陆名： </label><input type="text" name="condition[loginName|like]"/>
								</li>
								<li>
									<a class="btn btn-primary btn-rounded btn-sm" onclick="searchPre()"><i class="fa fa-search"></i>&nbsp;搜索</a>
									<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
								</li>
							</ul>
						</div>
					</form>
				</div>
				
				<div class="btn-group-sm" id="toolbar" role="group">
					<a class="btn btn-success" onclick="$.operate.add()">
						<i class="fa fa-plus"></i> 新增
					</a>
					<a class="btn btn-danger btn-del disabled" onclick="$.operate.removeAll()">
			            <i class="far fa-trash-alt"></i> 删除
			        </a>
			        <!-- 
			        <a class="btn btn-danger" onclick="$.operate.clean()">
		                <i class="fa fa-trash"></i> 清空
		            </a>
			         -->
		            <a class="btn btn-warning" onclick="$.table.exportExcel()">
			            <i class="fa fa-download"></i> 导出
			        </a>
		        </div>
		        
				<div class="col-sm-12 select-table table-striped">
					<table id="bootstrap-table" data-mobile-responsive="true"></table>
				</div>
				
			</div>
			
			
		</div>
	</div>
	
	<footer th:replace="dashboard/footer :: footer"></footer>
	
	<script type="text/javascript" th:src="@{/asserts/js/plugins/ztree/js/jquery.ztree.core.js}"></script>
	<script type="text/javascript" th:src="@{/asserts/js/plugins/ztree/js/jquery.ztree.excheck.js}"></script>
	<script type="text/javascript" th:src="@{/asserts/js/plugins/ztree/js/jquery.ztree.exedit.js}"></script>

	<script type="text/javascript">
		var setting = {
			async : {
				enable : true,
				type: "get" , 
				url: ctx+"portal/ucenter/department/departmentData?id=536478251871109120"
			},
			data : {
				simpleData : {
					enable : true,
					idKey : "id",
					pIdKey : "pid",
					rootPId : 0
				},
				key : {
					name : "fullName"
				}
			},
			callback : {
				onClick : zTreeOnClick 
			}
		};
		
		/**
		 * 刷新当前节点
		 */
		function refreshNode() {
			/*根据 treeId 获取 zTree 对象*/
			var zTree = $.fn.zTree.getZTreeObj("scriptTree"),
			type = "refresh",
			silent = false,
			/*获取 zTree 当前被选中的节点数据集合*/
			nodes = zTree.getSelectedNodes();
			/*强行异步加载父节点的子节点。[setting.async.enable = true 时有效]*/
			zTree.reAsyncChildNodes(nodes[0], type, silent);
		}
	 
		/**
		 * 刷新当前选择节点的父节点
		 */
		function refreshParentNode() {
			var zTree = $.fn.zTree.getZTreeObj("scriptTree"),
			type = "refresh",
			silent = false,
			nodes = zTree.getSelectedNodes();
			/*根据 zTree 的唯一标识 tId 快速获取节点 JSON 数据对象*/
			var parentNode = zTree.getNodeByTId(nodes[0].parentTId);
			/*选中指定节点*/
			zTree.selectNode(parentNode);
			zTree.reAsyncChildNodes(parentNode, type, silent);
		}

		function zTreeOnClick(event, treeId, treeNode) {
			console.log("tree node id = " + treeNode.id) ; 
			$("input[id=selectResourceParentNode]").attr("data-id" ,treeNode.id) ; 
			$("input[id=selectResourceParentNode]").attr("data-resourceName" ,treeNode.resourceName) ; 
		};


		$(document).ready(function() {
			var treeObj = $.fn.zTree.init($("#treeDemo"), setting);
			treeObj.expandAll(false);
		});
		
		$(function() {
		    var options = {
		        url: ctx+"portal/ucenter/account/datatables" ,
		        createUrl: ctx + 'portal/ucenter/account/add' ,
		        updateUrl: ctx+"portal/ucenter/account/modify?id={id}" ,
		        statusUrl: ctx+"portal/ucenter/account/changeStatus?id={id}" , 
		        detailUrl: ctx+"portal/ucenter/account/detail?id={id}",
		        removeUrl: ctx+"portal/ucenter/account/delete" ,
		        exportUrl: ctx + "portal/ucenter/account/exportExcel",
		        sortName: "operTime",
		        uniqueId:"id" ,
		        sortOrder: "desc",
		        modalName: "账户",
		        escape: true,
		        showPageGo: true,
		        rememberSelected: true,
		        columns: [{field: 'state',checkbox: true },
					{field: 'id', title: '序号' , align:'center' , width:'30px', formatter: function(v , r , i){return i + 1 ;}},
					{field: 'applicationIdLabel', title: '所属应用'},
					{field: 'avatorPath', title: '头像' , width:'60px',  align: 'center',  formatter: function(value, row, index) {
							var actions = [];
							if($.common.isEmpty(value)){
								actions.push('<img src="'+ctx+'asserts/img/header.png" class="project-icons-list" aria-hidden="true"></i>');
							}else{
								actions.push('<img src="'+value+'" class="project-icons-list" aria-hidden="true"></i>');
							}
							return actions.join('');
					  }
					},
					{field: 'name', title: '用户名'},
			        {field: 'loginName', title: '登陆名'},
			        {field: 'id', title: '角色' , formatter:function(v , r , i){
			                var actions = [];
			                
			                actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="authorizationItem(\'' + r.id + '\')"><i class="fa fa-leaf"></i> 授权</a> ');
			                
			                return actions.join('');
						}
			        },
			        {field: 'hasStatusLabel', title: '状态', align: 'center',  formatter:function(value,row,index){ return $.table.status(value , row);}},
			        {field: 'id' , title: '操作',  align: 'center',  formatter: function(value, row, index) {
			                var actions = [];
			                
			                actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i> 编辑</a> ');
			                actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="far fa-trash-alt"></i> 删除</a> ');
			                
			                return actions.join('');
			          }
			     }]
		    };
		    $.table.init(options);
		});
		
		function openDept(){
			$.modal.open("添加部门", ctx + "portal/ucenter/account/add_dept") ; 
		}
		
		// 授权 
		function authorizationItem(rowsId) {
			if(rowsId == null){
				layer.alert("请选择元素.") ; 
				return ; 
			}
			
			layer.open({
				type : 2,
				title : '用户授权',
				shadeClose : true,
				shade : 0.8,
				area : [ '500px', '700px' ],
				maxmin : true, //开启最大化最小化按钮
				content : ctx+'portal/ucenter/account/auth?accountId='+rowsId  , 
				btn: ['确认', '取消'] , 
				yes: function(index, layero){
					console.log("index = " + index + " layero = " + layero) ; 
					var selectResourceParentNode = $(layero).find("iframe")[0].contentWindow.document.getElementById("selectResourceParentNode")
					
					var rolesId = $(selectResourceParentNode).attr("data-id") ; 	
					
					console.log("rolesId = " + rolesId ) ; 
					$.post(ctx+"portal/ucenter/account/authAccount", {
						accountId :rowsId , 
						rolesId: rolesId 
					}, function(response) {
						console.log('response = ' + response);
						if(response.code != 200){
							layer.alert(response.message) ; 
						} else {
							layer.msg("操作成功.")	
						}
					})
					
					layer.close(index) ; 
			 	}
			});
		}
	</script>
</body>
</html>